# 将2个Series对象作为字典的值，就可以创建一个DataFrame对象
import pandas as pd

# Python原生的字典对象
population_dict = {'beijing': 200, 'shanghai': 300, 'nanjing': 400}
# Pandas Series对象
population_series = pd.Series(population_dict)

area_dict = {'beijing': 2000, 'shanghai': 3000, 'nanjing': 4000}
area_series = pd.Series(area_dict)

# 通过Pandas Series创建Pandas DataFrame对象
data_frame = pd.DataFrame({'population': population_series, 'area': area_series})
print(data_frame)

# Pandas DataFrame shape属性，复用Numpy的shape
print(data_frame.shape)

# Pandas DataFrame列对象
print(data_frame.index)
# Pandas DataFrame行对象
print(data_frame.columns)
print(data_frame.values)

# 默认返回行数为5: n: int = 5
print(data_frame.head(n=2))
print(data_frame.tail(n=1))

# 不能通过Python Dict创建Pandas DataFrame对象，会报错 TypeError: unhashable type: 'dict'
# data_frame_dict = pd.DataFrame({area_dict, population_dict})
# print(data_frame_dict)

# 创建Pandas DataFrame对象，指定列索引的名称
pd_data_frame = pd.DataFrame(population_series, columns=['population'])
print(pd_data_frame)

# 重设DataFrame索引 TODO 待确定具体使用场景
set_pd_data_frame = pd_data_frame.set_index("population", drop=False)
print(set_pd_data_frame)


# 重置DataFrame索引
reset_pd_data_frame = pd_data_frame.reset_index()
print(reset_pd_data_frame)
